#!/bin/sh

function pwm_init() {
PATH="/sys/class/pwm"
pmws=(0 1 2 3 4 5 6 7 8 9)
inversed=true
for i in ${pmws[*]}
do
    NUM=$((${i}%2))
    CHIP=$((${i}/2))
    PWM_CHIP="pwmchip$((${CHIP}*2))"
    PWM_PATH="${PATH}/pwm${i}"
    PWM_CHIP_PATH="${PATH}/${PWM_CHIP}"
    if [ ! -e "${PWM_PATH}" ]
    then
        if [ -e "${PWM_CHIP_PATH}" ]
        then
            echo ${NUM} > ${PWM_CHIP_PATH}/export
            echo $1 > ${PWM_PATH}/period
            if [ ${i} -eq 5 ] ||  [ ${i} -eq 7 ]
            then
                echo $1 > ${PWM_PATH}/duty_cycle
            else
                echo $2 > ${PWM_PATH}/duty_cycle
            fi
            if [ ${NUM} -eq 1 ]
                then
                    echo normal > ${PWM_PATH}/polarity
                else
					echo inversed > ${PWM_PATH}/polarity
                fi
            echo 1  > ${PWM_PATH}/enable
        else
            echo "${PWM_CHIP_PATH} is not found !!!"
        fi
    else
        echo 0  > ${PWM_PATH}/enable
        echo $1 > ${PWM_PATH}/period
                if [ ${i} -eq 5 ] ||  [ ${i} -eq 7 ]
                then
                    echo $1 > ${PWM_PATH}/duty_cycle
                else
                    echo $2 > ${PWM_PATH}/duty_cycle
                fi
                if [ ${NUM} -eq 1 ]
                then
                        echo normal > ${PWM_PATH}/polarity
                else
                        echo inversed > ${PWM_PATH}/polarity
                fi
        echo 1  > ${PWM_PATH}/enable
    fi
done
}
pwm_init 40000 24000
exit 0
if [ $# -eq 2 ]
then
        if [ $1 -ge $2 -a $1 -gt 20000 ]
        then
                if [ $1 -ge $2 ]
                then
                        pwm_init $1 $2
                fi
        fi
else
        pwm_init 40000 24000
fi
